<template>
	<cl-crud :ref="setRefs('crud')" @load="onLoad">
		<el-row type="flex" align="middle">
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<cl-filter label="渠道">
				<el-select size="mini" v-model="query.source" placeholder="渠道">
					<el-option
						v-for="item in sourceList"
						:key="item.value"
						:label="item.label"
						:value="item.value"
					>
					</el-option>
				</el-select>
			</cl-filter>
			<!-- 关键字搜索 -->
			<cl-search-key :on-search="onSearch" />
		</el-row>

		<el-row>
			<!-- 数据表格 -->
			<cl-table :ref="setRefs('table')" v-bind="table">
                <template #slot-btn="{ scope }">
                    <el-button
						type="text"
						size="mini"
						style="margin-left: 8px"
						target="_blank"
						@click="toMagic(scope.row.target_id)"
						>编辑模板</el-button
					> 
                </template>
            </cl-table>
		</el-row>

		<el-row type="flex">
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</el-row>

		<!-- 新增、编辑 -->
		<cl-upsert :ref="setRefs('upsert')" v-bind="upsert" @open="onOpen">
			<template #slot-targetId="{ scope }">
				<el-select
					v-model="scope.targetId"
					filterable
					remote
					reserve-keyword
					placeholder="搜索页面"
					:remote-method="remoteMethod"
					:loading="loading"
				>
					<el-option
						v-for="item in magicList"
						:key="item.id"
						:label="item.title"
						:value="item.id"
					>
						<span style="float: left">{{ item.title }}</span>
						<span
							style="
								float: right;
								color: var(--el-text-color-secondary);
								font-size: 13px;
							"
							>{{ item.source }}</span
						>
					</el-option>
				</el-select>
			</template>
		</cl-upsert>
	</cl-crud>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue";
import { CrudLoad, Upsert, Table } from "cl-admin-crud-vue3/types";
import { useCool } from "/@/core";
import { source } from "/$/magic/lib/source";

export default defineComponent({
	setup() {
		// 功能
		const { refs, setRefs, service ,router} = useCool();
		const sourceList = ref(source);

		const query = reactive<any>({
			taget_id: null,
			source: null
		});

		// 新增、编辑配置
		const upsert = reactive<Upsert>({
			items: [
				{
					label: "名称",
					prop: "name",
					component: {
						name: "el-input",
						props: {
							clearable: true
						},
						attrs: {
							placeholder: "请输入名称"
						}
					},
					rules: {
						required: true,
						message: "名称不能为空"
					}
				},
				{
					label: "关联页面",
					prop: "targetId",
					component: {
						name: "slot-targetId"
					}
				},
				{
					label: "渠道",
					prop: "source",
					value: "default",
					component: {
						name: "el-select",
						attrs: {
							placeholder: "选择使用渠道"
						},
						options: sourceList
					},
					rules: {
						required: true,
						message: "不能为空"
					}
				},
				{
					label: "备注",
					prop: "remark",
					component: {
						name: "el-input",
						props: {
							clearable: true
						},
						attrs: {
							placeholder: "请输入名称"
						}
					}
				},
				{
					label: "排序",
					prop: "sort",
					component: {
						name: "el-input-number"
					}
				},
				{
					label: "状态",
					span: 12,
					prop: "status",
					component: {
						name: "el-switch",
						props: {
							"active-value": 1,
							"inactive-value": 0
						}
					}
				}
			]
		});

		// 表格配置
		const table = reactive<Table>({
			columns: [
				{
					type: "selection",
					width: 60
				},
				{
					label: "ID",
					prop: "id"
				},

				{
					label: "名称",
					prop: "name"
				},
				{
					label: "页面标题",
					prop: "magic_title",
					align: "center"
				},
				{
					label: "渠道",
					prop: "source",
					align: "center",
					dict: sourceList
				},
				{
					label: "状态",
					prop: "status",
					dict: [
						{
							label: "开启",
							value: 1,
							type: "success"
						},
						{
							label: "关闭",
							value: 0,
							type: "danger"
						}
					],
					minWidth: 100
				},
                {
					label: "排序",
					prop: "sort",
					sortable: "custom",
				},
				{
					label: "创建时间",
					prop: "createTime",
					sortable: "custom",
					minWidth: 150
				},
				{
					label: "操作",
					type: "op",
					buttons: ["edit", "delete",'slot-btn']
				}
			]
		});

		// crud 加载
		function onLoad({ ctx, app }: CrudLoad) {
			// 绑定 service
			ctx.service(service.magic.magicCate).done();
			app.refresh();
		}
		function onSearch(params: any, { next }: any) {
			next(query);
		}

        function onOpen() {
			console.log("open");
            getMagiclist('')
		}

		const magicList = ref<any[]>([]);
		const loading = ref<boolean>(false);

		function remoteMethod(params: any) {
			console.log(params);
			loading.value = true;
            getMagiclist(params)
		}

      async  function  getMagiclist(params: any){
          service.magic.magic.page({ keyWord: params }).then((res: any) => {
				magicList.value = res.list;
				loading.value = false;
				console.log(magicList, 123);
			});
        }
        
		function toMagic(id: number) {
			let routeData = router.resolve({ path: "/magic", query: { id } });
			window.open(routeData.href, "_blank");
		}
		return {
			refs,
			setRefs,
			upsert,
			table,
			sourceList,
			magicList,
			query,
			remoteMethod,
            toMagic,
			onSearch,
            onOpen,
			onLoad
		};
	}
});
</script>
